import { Guid } from './src/common/utils/guid';
const gulp = require('gulp');
import { CodeGenerator } from 'tiny-entity2';
import * as fs from 'fs';
import * as path from 'path';
import { ComponentService } from './src/service/comService';
const ts = require('gulp-typescript');
const tsProj = ts.createProject('tsconfig.json');
// process.env.tinyLog = 'on';

let apiGenerator = new CodeGenerator({
    outDir: path.resolve('./src/domain'),
    modelLoadPath: [
        path.resolve('./src/domain/models')
    ],
    modelExportPath: [
        './models'
    ],
    ctxExportPath: '',
    configFilePath: './dbConfig',
    outFileName: 'apiDataContext.ts',
    databaseType: 'mysql',
    packageName: 'tiny-entity2/mysql/dataContextMysql'
});

gulp.task('gctx', async () => {
    apiGenerator.generateCtxFile();
})

gulp.task('gdb', () => {
    let index = process.argv.findIndex(x => x == 'gdb');
    let p = process.argv[index + 1];
    if (!p) {
        p = '-api';
    }
    if (p == '-api') {
        apiGenerator.entityToDatabase();
    }
})

gulp.task('guid', () => {
    console.log(Guid.GetGuid(), Date.now());
});

gulp.task('initcom', async () => {
    try {
        let svr = new ComponentService();
        await svr.InitDefaultComponent();
        await svr.InitDataSource();
    } catch (error) {
        console.error(error);
    }
});

gulp.task('tsc', () => {
    var result = tsProj.src().pipe(tsProj());
    return result.pipe(gulp.dest('./'));
});

gulp.task('gop', async () => {
    let index = process.argv.findIndex(x => x == 'gop');
    console.log(process.argv);
    let p = process.argv[index + 1];
    if (!p) {
        p = '-api';
    }
    if (p == '-api') {
        await apiGenerator.generateOpLogFile();
        await apiGenerator.sqlLogToDatabase();
    }
    console.log('gop successful!');
    process.exit(0);
});

gulp.task('build', ['tsc'], () => {
    gulp.src('./src/**/*.js').pipe(gulp.dest('./build/src'));
    gulp.src('./src/**/*.json').pipe(gulp.dest('./build/src'));

    gulp.src('./gulpfile.js').pipe(gulp.dest('./build'));
    // gulp.src('./sync.start.js').pipe(gulp.dest('./build'));
    gulp.src('./api.start.js').pipe(gulp.dest('./build'));
    gulp.src('./module.start.js').pipe(gulp.dest('./build'));
    // gulp.src('./grayscale.start.js').pipe(gulp.dest('./build'));
    gulp.src('./README.md').pipe(gulp.dest('./build'));
    gulp.src('./certificate/**/*.*').pipe(gulp.dest('./build/certificate'));

    let index = process.argv.findIndex(x => x == '--ver');
    let version = "";
    if (index > -1) {
        version = process.argv[index + 1];
    }

    let buf = fs.readFileSync('./package.json');
    let packageJson = JSON.parse(buf.toString());
    if (version) {
        packageJson.version = version;
    }
    else {
        let versionFormatArray = packageJson.version.split('.');
        versionFormatArray[versionFormatArray.length - 1] = parseInt(versionFormatArray[versionFormatArray.length - 1]) + 1;
        packageJson.version = versionFormatArray.join('.');
    }
    fs.writeFileSync('./package.json', JSON.stringify(packageJson));
    if (!fs.existsSync('./build')) {
        fs.mkdirSync('./build');
    }
    fs.writeFileSync('./build/package.json', JSON.stringify(packageJson));

    // process.exit(0);
})